package com.jpress.model;


import java.util.Date;
import java.util.List;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;


@Entity
@Table(name="blog")
@NamedQueries({
	@NamedQuery(name="Blog.countBlogs", query="SELECT COUNT(b) FROM Blog b"),
	@NamedQuery(name="Blog.findByNome", query="SELECT b FROM Blog b WHERE b.nome = :nome"),
	@NamedQuery(name="Blog.SearchByNome", query="SELECT b FROM Blog b WHERE b.nome LIKE :nome ORDER BY b.nome ASC"),
	@NamedQuery(name="Blog.SearchByTitulo", query="SELECT b FROM Blog b WHERE b.titulo LIKE :titulo"),
	@NamedQuery(name="Blog.SearchByDescricao", query="SELECT b FROM Blog b WHERE b.descricao LIKE :descricao"),
	@NamedQuery(name="Blog.findAll", query="SELECT b FROM Blog b"),
	@NamedQuery(name="Blog.findAllDesc", query="SELECT b FROM Blog b ORDER BY b.id DESC"),
	@NamedQuery(name="Blog.Search", query="SELECT b FROM Blog b WHERE b.nome LIKE :nome AND b.titulo LIKE :titulo AND b.descricao LIKE :descricao")
})
public class Blog {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="codigo")
	private int codigo;
	
	@Column(name="nome", length=100, unique=true, nullable=false)
	private String nome;
	
	@Column(name="titulo", length=100, nullable=false)
	private String titulo;
	
	@Column(name="descricao", length=255, nullable=false)
	private String descricao;
	
	@Column(name="avatar", length=1000)
	private String avatar;
	
	@OneToOne
	private Usuario dono;
	
	@Column(name="dataCriacao", nullable=false)
	@Temporal(TemporalType.DATE)
	private Date dataCriacao;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="blog")
	private List<Post> posts;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="blog")
	private List<CustomPage> customPages;
	
	public Blog(){}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Usuario getDono() {
		return dono;
	}

	public void setDono(Usuario dono) {
		this.dono = dono;
	}

	public String getNomeDono(){
		return this.dono.getNome();
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	public void adicionarPost(Post post){
		this.posts.add(post);
	}


	public List<CustomPage> getCustomPages() {
		return customPages;
	}

	public void setCustomPages(List<CustomPage> customPages) {
		this.customPages = customPages;
	}

	public void adicionarCustomPage(CustomPage customPage){
		this.customPages.add(customPage);
	}

	public Date getDataCriacao() {
		return dataCriacao;
	}

	public void setDataCriacao(Date dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}
}